package com.decorate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.decorate.Vos.DesignTeamReqVo;
import com.decorate.Vos.DesignTeamVo;
import com.decorate.Vos.DesignerVo;
import com.decorate.Vos.R;
import com.decorate.pojo.ArchitectStyle;
import com.decorate.pojo.DesignCompany;
import com.decorate.pojo.Designer;
import com.decorate.pojo.SysDict;
import com.decorate.service.ArchitectStyleService;
import com.decorate.service.DesignCompanyService;
import com.decorate.service.DesignerService;
import com.decorate.mapper.DesignerMapper;
import com.decorate.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author duji
* @description 针对表【designer(设计师)】的数据库操作Service实现
* @createDate 2022-08-23 22:36:28
*/
@Service
public class DesignerServiceImpl extends ServiceImpl<DesignerMapper, Designer> implements DesignerService{

    //设计师表 Mapper
    @Autowired
    public DesignerMapper designerMapper;

    //设计师-风格中间表 Mapper
    @Autowired
    public ArchitectStyleService architectStyleService;

    //设计公司表 Mapper
    @Autowired
    public DesignCompanyService designCompanyService;

    //字典表
    @Autowired
    public SysDictService sysDictService;

    @Override
    public R<DesignerVo> designer(Integer id) {
        //根据设计师id查询设计师类
        Designer designer = designerMapper.selectById(id);
        //设计师类存入DesignerVo
        DesignerVo designerVo = new DesignerVo();
        designerVo.setDesignId(designer.getId());
        designerVo.setName(designer.getName());
        designerVo.setIdentity(designer.getIdentity());
        designerVo.setDesignPhilosophy(designer.getDesignPhilosophy());
        designerVo.setExperience(designer.getExperience());
        designerVo.setProductNumbers(designer.getProductNumbers());
        designerVo.setRepresentativeWorks(designer.getRepresentativeWorks());
        String styles = getStyles(designer.getId());
        designerVo.setStyle(styles);
        DesignCompany company = getCompany(designer.getCompanyId());
        designerVo.setCompanyAddress(company.getAddress());
        designerVo.setCompanyCity(company.getCity());
        designerVo.setCompanyName(company.getName());
        return R.ok(designerVo);
    }

    @Override
    public R<List<DesignTeamVo>> designTeams(DesignTeamReqVo designTeamReqVo) {
        //新建查询条件
        LambdaQueryWrapper<Designer> wrapper = new LambdaQueryWrapper<>();
        //是否禁用
        wrapper.eq(Designer::getIsDelete,"0");
        //风格
        if(!designTeamReqVo.getStyle().isEmpty()){
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictValue, designTeamReqVo.getStyle()));
            List<ArchitectStyle> list = architectStyleService.list(Wrappers.<ArchitectStyle>lambdaQuery().eq(ArchitectStyle::getDictLabel, dict.getDictLabel()));
            ArrayList<Integer> ids = new ArrayList<>();
            for (ArchitectStyle architectStyle : list) {
                ids.add(architectStyle.getArchitectId());
            }
            wrapper.in(Designer::getId,ids);
        }
        //身份
        if(!designTeamReqVo.getIdentity().isEmpty()){
            wrapper.eq(Designer::getIdentity, designTeamReqVo.getIdentity());
        }
        //城市
        if(!designTeamReqVo.getCompanyCity().isEmpty()){
            List<DesignCompany> list = designCompanyService.list(Wrappers.<DesignCompany>lambdaQuery().eq(DesignCompany::getCity, designTeamReqVo.getCompanyCity()));
            ArrayList<Integer> ids = new ArrayList<>();
            for (DesignCompany designCompany : list) {
                ids.add(designCompany.getId());
            }
            wrapper.eq(Designer::getCompanyId,ids);
        }
        //根据条件查询设计师集合
        List<Designer> designers = designerMapper.selectList(wrapper);
        List<DesignTeamVo> designTeams = new ArrayList<>();
        for (Designer designer : designers) {
            DesignTeamVo designTeamVo = new DesignTeamVo();
            designTeamVo.setDesignId(designer.getId());
            String styles = getStyles(designer.getId());
            DesignCompany designCompany = getCompany(designer.getCompanyId());
            designTeamVo.setName(designer.getName());
            designTeamVo.setIdentity(designer.getIdentity());
            designTeamVo.setProductNumbers(designer.getProductNumbers());
            designTeamVo.setStyle(styles);
            designTeamVo.setCompany(designCompany.getName());
            designTeams.add(designTeamVo);
        }
        return R.ok(designTeams);
    }

    //根据公司id查询设计公司类
    private DesignCompany getCompany(Integer companyId) {
        DesignCompany company = designCompanyService.getById(companyId);
        return company;
    }

    //根据设计师id查询风格字符串
    public String getStyles(Integer id){
        //查询设计师--风格中间表，查询设计师擅长的风格id
        String s = null;
        List<ArchitectStyle> styleList = architectStyleService.list(Wrappers.<ArchitectStyle>lambdaQuery().eq(ArchitectStyle::getArchitectId, id).eq(ArchitectStyle::getIsDelete,"0"));
        for (ArchitectStyle architectStyle : styleList) {
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictLabel, architectStyle.getDictLabel()));
            s += dict.getDictValue() + ",";
        }
        return s.substring(0,s.length()-1);
    }
}
